package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.CodeConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Program: review2-health
 * @ClassName: CheckItemController
 * @Description:
 * @Author: KyleSun
 **/
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;


    @GetMapping
    public Result findAll() {
        List<CheckItem> checkItemList = checkItemService.findAll();
        return new Result(true, "SUCCESS", checkItemList);
    }


    @GetMapping("/{id}")
    public Result findOne(@PathVariable("id") Integer id) {
        CheckItem checkItem = checkItemService.findOne(id);
        return new Result(true, "SUCCESS", checkItem);
    }


    @PostMapping
    public Result add(@RequestBody CheckItem checkItem) {
        CheckItem item = checkItemService.add(checkItem);
        return new Result(true, "SUCCESS", item);
    }


    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        checkItemService.delete(id);
        return new Result(true, "SUCCESS");
    }


    @PutMapping("/{id}")
    public Result update(@PathVariable("id") Integer id, @RequestBody CheckItem checkItem) {
        CheckItem item = checkItemService.update(id, checkItem);
        return new Result(true, "SUCCESS", item);
    }


    @PostMapping("/{page}/{size}")
    public Result update(@RequestBody CheckItem checkItem,
                         @PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        PageResult<CheckItem> pageResult = checkItemService.search(checkItem, page, size);
        return new Result(true, "SUCCESS", pageResult);
    }
}
